<template>
  <div class="box">
    <div class="login-box">
      <div class="logo">
        <img src="../assets/images/logo.png" alt />
      </div>
      <el-form
        :model="ruleForm2"
        status-icon
        :rules="rules2"
        ref="ruleForm2"
        label-width="60px"
        size="medium"
      >
        <el-form-item label="用户名" prop="pass">
          <el-input type="text" v-model="ruleForm2.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        pass: "admin",
        checkPass: "123456",
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this;
          let t = async function () {
            const { data: res } = await _this.$axios.post("/login", {
              username: _this.ruleForm2.pass,
              password: _this.ruleForm2.checkPass,
            });
            window.sessionStorage.setItem("token", res.data.token);
            
            _this.$router.push("/nav");
          };
          t();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.children[0].focus();
      },
    },
  },
  created() {},
  inserted() {},
  mounted() {
    // let _this = this;
    // const ad = async function () {
    //   const { data: res } = await _this.$axios.post("/login", {
    //     username: "admin",
    //     password: "123456",
    //   });
    //   console.log(res);
    //   window.sessionStorage.setItem("token", res.data.token);
    // };
    // ad();
    // 记录后端返回的token存在sessionStorage，用于后面请求的验证
    // window.sessionStorage.setItem("token", res.data.token)
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
.box {
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  .login-box {
    padding: 0 25px;
    width: 400px;
    height: 340px;
    background-color: #ffffff;
    border-radius: 5px;
    margin: auto;
    position: relative;
    .logo {
      position: absolute;
      width: 150px;
      height: 150px;
      border-radius: 50%;
      z-index: 1;
      display: flex;
      top: -65px;
      left: 150px;
      box-shadow: 0px 0px 20px 7px #7d6c6c;
      background-color: #ffffff;
      img {
        background-color: #eeeeee;
        width: 130px;
        height: 130px;
        border-radius: 50%;
        margin: auto;
      }
    }
    .el-form {
      position: absolute;
      bottom: 0;
      width: 80%;
    }
  }
}
</style>